package com.gc.web.controller.business;

import java.util.List;
import java.util.Arrays;
import java.util.Map;

import com.gc.common.base.Result;
import com.gc.common.support.Convert;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.gc.common.annotation.Log;
import com.gc.common.enums.BusinessType;
import com.gc.business.domain.AdminRegion;
import com.gc.business.service.IAdminRegionService;
import com.gc.framework.web.base.BaseController;
import com.gc.common.utils.poi.ExcelUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * 行政区域管理 控制层
 *
 * @author hruiyan
 * @date 2020-11-12
 */
@Slf4j
@Controller
@RequestMapping("/business/adminRegion")
public class AdminRegionController extends BaseController {
    private String prefix = "business/adminRegion";

    private final IAdminRegionService adminRegionService;

    @Autowired
    public AdminRegionController(IAdminRegionService adminRegionService) {
        this.adminRegionService = adminRegionService;
    }

    /**
     * 列表页
     */
    @RequiresPermissions("business:adminRegion:view")
    @GetMapping
    public String adminRegion() {
        return prefix + "/adminRegion";
    }

    /**
     * 新增页
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 编辑页
     */
    @GetMapping("/edit/{regionId}")
    public String edit(@PathVariable("regionId") Long regionId, ModelMap mmap) {
        AdminRegion adminRegion = adminRegionService.getById(regionId);
        mmap.put("adminRegion", adminRegion);
        return prefix + "/edit";
    }

    /**
     * 查询行政区域管理列表
     */
    @RequiresPermissions("business:adminRegion:list")
    @PostMapping("/list")
    @ResponseBody
    public Result list(AdminRegion adminRegion) {
        startPage();
        List<AdminRegion> list = adminRegionService.selectAdminRegion();
        return Result.success(getDataTable(list));
    }

    /**
     * 新增保存行政区域管理
     */
    @RequiresPermissions("business:adminRegion:add")
    @Log(title = "行政区域管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public Result addSave(AdminRegion adminRegion) {
        adminRegionService.save(adminRegion);
        return Result.success();
    }

    /**
     * 修改保存行政区域管理
     */
    @RequiresPermissions("business:adminRegion:edit")
    @Log(title = "行政区域管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public Result editSave(AdminRegion adminRegion) {
        adminRegionService.updateById(adminRegion);
        return Result.success();
    }

    /**
     * 删除行政区域管理
     */
    @RequiresPermissions("business:adminRegion:remove")
    @Log(title = "行政区域管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public Result remove(String ids) {
        Long[] longIds = Convert.toLongArray(ids);
        adminRegionService.removeByIds(Arrays.asList(longIds));
        return Result.success();
    }

    /**
     * 导出行政区域管理列表
     */
    @RequiresPermissions("business:adminRegion:export")
    @PostMapping("/export")
    @ResponseBody
    public Result export(AdminRegion adminRegion) {
        List<AdminRegion> list = adminRegionService.list(adminRegion);
        ExcelUtil<AdminRegion> util = new ExcelUtil<>(AdminRegion.class);
        util.exportExcel(list, "adminRegion");
        return Result.success();
    }

    /**
     * 树结构列表
     * @return
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Map<String, Object>> treeData(Long attrId){
        return adminRegionService.selectDeptTree(attrId);
    }

}
